package com.whmskj.xjlhsc.pc.productionreport.task.day;

import com.whmskj.xjlhsc.pc.productionreport.task.mapper.ProductionMapper;
import com.whmskj.xjlhsc.pc.productionreport.task.pojo.DayVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;

/**
 * @author jiaruchun
 * @date 2025-09-16 11:16
 */
@Slf4j
@Component
@EnableScheduling
public class DayTask {
    @Autowired
    private ProductionMapper productionMapper;
    //每天早上10点执行
    @Scheduled(cron = "0 0 10 * * ?")
    //每隔5秒执行
//    @Scheduled(cron = "0/5 * * * * ?")
    public void dayTask(){
        List<DayVO> dayProductionData = productionMapper.getDayProductionData();
        for (DayVO dayProductionDatum : dayProductionData) {
            //四舍五入保留两位小数
            dayProductionDatum.setEarlyEfficiency(Math.round(dayProductionDatum.getEarlyEfficiency() * 100.0) / 100.0f);
            dayProductionDatum.setLateEfficiency(Math.round(dayProductionDatum.getLateEfficiency() * 100.0) / 100.0f);
            dayProductionDatum.setTotalKg(Math.round(dayProductionDatum.getTotalKg() * 100.0) / 100.0f);
            dayProductionDatum.setEarlyKg(Math.round(dayProductionDatum.getEarlyKg() * 100.0) / 100.0f);
            dayProductionDatum.setLateKg(Math.round(dayProductionDatum.getLateKg() * 100.0) / 100.0f);
            dayProductionDatum.setEarlyKm(Math.round(dayProductionDatum.getEarlyKm() * 100.0) / 100.0f);
            dayProductionDatum.setLateKm(Math.round(dayProductionDatum.getLateKm() * 100.0) / 100.0f);
        }
        log.info("任务执行，数据量：{}",dayProductionData.size());
        //插入中间表,实现幂等性,判断昨天的数据有无，无则插入
        Integer dayProductionDataByDate = productionMapper.getDayProductionDataByDate(LocalDate.now().minusDays(1).toString());
        if (dayProductionDataByDate == 0) {
            productionMapper.insertDayProductionData(dayProductionData);
        }
    }

}
